home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / glibc108.gz / glibc108 / glibc-1.08.1 / manual / examples / mkisock.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  607b  |  32 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5.  
  6. int 
  7. make_socket (unsigned short int port)
  8. {
  9.   int sock;
  10.   struct sockaddr_in name;
  11.  
  12.   /* Create the socket. */
  13.   sock = socket (PF_INET, SOCK_STREAM, 0);
  14.   if (sock < 0)
  15.     {
  16.       perror ("socket");
  17.       exit (EXIT_FAILURE);
  18.     }
  19.  
  20.   /* Give the socket a name. */
  21.   name.sin_family = AF_INET;
  22.   name.sin_port = htons (port);
  23.   name.sin_addr.s_addr = htonl (INADDR_ANY);
  24.   if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
  25.     {
  26.       perror ("bind");
  27.       exit (EXIT_FAILURE);
  28.     }
  29.  
  30.   return sock;
  31. }
  32.